package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController {
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private UserFollowService userFollowService;

    ///house/list/'+pageNum+'/'+this.page.pageSize, this.houseQueryVo
    @RequestMapping("/list/{pageNum}/{pageSize}")
    @ResponseBody
    public Result<PageInfo<HouseVo>> findListPage(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestBody HouseQueryVo houseQueryVo
    ){

        // 调用服务层方法,带条件分页查询信息
        PageInfo<HouseVo> pageInfo =houseService.findListPage(pageNum,pageSize,houseQueryVo);

        // 将PageInfo对象放入Result返回
        return Result.ok(pageInfo);
    }

    /*前台获取房源详情的处理器*/
    @RequestMapping("/info/{id}")
    @ResponseBody
    public Result info(@PathVariable("id") Long id, HttpServletRequest request){
        // 房子信息 house
//        that.house = response.data.data.house
        House house = houseService.getById(id);
        // 小区 信息 community
//        that.community = response.data.data.community
        Community community = communityService.getById(house.getCommunityId());
        // 经纪人信息 broker
//        that.houseBroker = response.data.data.houseBrokerList[0]
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        // 房源图片 houseImage
//        that.houseImage1List = response.data.data.houseImage1List
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        // 关注房源 follow
        // 查看当前用户是否关注了该房源
        boolean isFollow=false;
        HttpSession session = request.getSession();
        UserInfo userInfo=(UserInfo)session.getAttribute("USER");
        if(null != userInfo){
            isFollow=userFollowService.isFollowed(userInfo.getId(),id);

        }


        Map<String,Object> map =new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
